import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:mahjong/const/MahjongConst.dart';
import 'package:mahjong/const/MahjongSystemConst.dart';
import 'package:mahjong/entity/card/MahjongCardEntity.dart';
import 'package:mahjong/notify/TopUserInfo.dart';
import 'package:mahjong/widget/CurrentGameCommons.dart';
import 'package:mahjong/widget/MahjongCommon.dart';
import 'package:provider/provider.dart';
import 'package:mahjong/entity/UserTouchBar.dart';
import 'SwapMahjongWidget.dart';

class TopGameWidget extends StatelessWidget {
  const TopGameWidget({
    Key key,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Consumer(builder:
        (BuildContext buildContext, TopUserInfo provider, Widget child) {
      if (provider.userId == null) {
        return Container();
      } else {
        return Stack(
          children: [
            // 头像的位置
            Positioned(
              top: 30.h,
              left: ScreenUtil().screenWidth -
                  MahjongSystemConst.lrUserCardMargin +
                  MahjongSystemConst.exportAreaCardWidth *
                      (MahjongSystemConst.cardMultiple + 2),
              child: SafeArea(
                child: MahjongCommon.getUserHeader(
                  provider,
                  key: userLocation[provider.userId].headKey,
                ),
              ),
            ),
            initReading(provider.prepare),

            provider.listening
                ? Positioned(
                    top: 30.h,
                    left: ScreenUtil().screenWidth -
                        MahjongSystemConst.lrUserCardMargin +
                        MahjongSystemConst.exportAreaCardWidth *
                            MahjongSystemConst.cardMultiple,
                    child: Image.asset(
                      "images/mahjong/action/CARD_CALL_TIPS.png",
                      height: MahjongSystemConst.actionTipsWidth,
                      width: MahjongSystemConst.actionTipsWidth,
                      fit: BoxFit.cover,
                    ))
                : Container(),
            // 出牌的位置
            Positioned(
                bottom: MahjongSystemConst.btExportMagin,
                left: ScreenUtil().screenWidth / 2 -
                    MahjongSystemConst.exportAreaSize / 2,
                child: RotationTransition(
                    turns: new AlwaysStoppedAnimation(180 / 360),
                    child:
                        MahjongCommon.getUserExportCard(provider, resv: true))),
            // 手上的牌的位置
            // 手牌位置
            Positioned(
                top: 30.h,
                left: ScreenUtil().screenWidth -
                    MahjongSystemConst.lrUserCardMargin +
                    MahjongSystemConst.exportAreaCardWidth * 2,
                child: Container(
                  alignment: Alignment.bottomLeft,
                  width: MahjongSystemConst.exportAreaCardWidth *
                      MahjongSystemConst.cardMultiple,
                  child: Row(
                      mainAxisAlignment: MainAxisAlignment.start,
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: [
                        Row(
                            mainAxisAlignment: MainAxisAlignment.start,
                            crossAxisAlignment: CrossAxisAlignment.end,
                            children: buildUserCard(provider,
                                MahjongSystemConst.exportAreaCardWidth)),
                        buildTouchBar(provider.userTouchBar),
                      ]),
                )),
            // 交换牌的位置
            SwapMahjongWidget(
              swap: provider.swap,
              position: UserPosition.TOP,
            ),
            Align(
              alignment: Alignment(0, -0.6),
              child: AnimatedOpacity(
                opacity:
                    (provider.userDoAction != null && provider.showDoAction)
                        ? 1
                        : 0,
                duration: Duration(seconds: 1),
                child: provider.showDoAction
                    ? Image.asset(
                        "images/mahjong/action_tips/" +
                            provider.userDoAction +
                            ".png",
                        height: MahjongSystemConst.actionTipsWidth,
                        width: MahjongSystemConst.actionTipsWidth,
                        fit: BoxFit.cover,
                      )
                    : SizedBox(),
              ),
            ),
          ],
        );
      }
    });
  }

  Widget buildTouchBar(List<UserTouchBar> data) {
    if (data == null) {
      return SizedBox();
    } else {
      List<Widget> param = [];
      data.forEach((element) {
        List<Widget> cards = [];
        for (int i = 0, j = element.cards.length; i < j; i++) {
          MahjongCardEntity cardEntity = element.cards[i];
          if (j == 4 && i < j - 1 && element.cardEnum == CardEnum.ANGANG) {
            cardEntity.cardId = null;
            cards.add(CardWidget.buildExportCard(
                cardEntity, MahjongSystemConst.exportAreaCardWidth));
          } else if (j == 4 &&
              i == j - 1 &&
              element.cardEnum == CardEnum.ANGANG) {
            cards.add(CardWidget.buildExportCard(
                cardEntity, MahjongSystemConst.exportAreaCardWidth));
          } else {
            cards.add(CardWidget.buildExportCard(
                cardEntity, MahjongSystemConst.exportAreaCardWidth));
          }
        }
        param.add(Padding(
            padding: EdgeInsets.only(
                left: MahjongSystemConst.exportAreaCardWidth / 2),
            child: Row(
              children: cards,
            )));
      });
      return Row(
        mainAxisAlignment: MainAxisAlignment.end,
        crossAxisAlignment: CrossAxisAlignment.end,
        children: param,
      );
    }
  }

  List<Widget> buildUserCard(TopUserInfo provider, double top) {
    List<Widget> result = [];
    var number = provider.cardCount;
    if (provider.win) {
      result.add(CardWidget.buildExportCard(
          MahjongCardEntity(cardId: provider.huCard),
          MahjongSystemConst.exportAreaCardWidth));
      result.add(SizedBox(width: MahjongSystemConst.exportAreaCardWidth / 2));
    } else if (number % 3 != 2) {
      result.add(SizedBox(width: MahjongSystemConst.exportAreaCardWidth));
      result.add(SizedBox(width: MahjongSystemConst.exportAreaCardWidth / 2));
    }
    for (int i = 0; i < number; i++) {
      if (number % 3 == 2 && i == 1) {
        result.add(SizedBox(width: MahjongSystemConst.exportAreaCardWidth / 2));
      }
      result.add(Container(
          width: MahjongSystemConst.exportAreaCardWidth,
          height: MahjongSystemConst.exportAreaCardWidth *
              MahjongSystemConst.cardWHScale,
          child: Image.asset(
            provider.win
                ? "images/mahjong/user/bottom/export_up.png"
                : "images/mahjong/user/top/usercard.png",
            fit: BoxFit.cover,
          )));
    }

    return result;
  }

  initReading(bool prepare) {
    return prepare
        ? Positioned(
            top: 30.h,
            left: ScreenUtil().screenWidth -
                MahjongSystemConst.lrUserCardMargin +
                MahjongSystemConst.exportAreaCardWidth *
                    MahjongSystemConst.cardMultiple +
                MahjongSystemConst.exportAreaCardWidth * 2 -
                MahjongSystemConst.userHeaderImageWidth,
            child: SafeArea(
              child: Container(
                height: MahjongSystemConst.userHeaderImageWidth *
                    MahjongSystemConst.cardMinWHScale,
                width: MahjongSystemConst.userHeaderImageWidth *
                    MahjongSystemConst.cardMinWHScale,
                child: Image.asset(
                  "images/mahjong/commons/reading.png",
                  fit: BoxFit.contain,
                ),
              ),
            ))
        : SizedBox();
  }
}
